package com.dc.bobaoge.project.v3.to.v2.business.core;

import com.dc.bobaoge.base.entity.message.core.po.MessageApprentice;
import com.dc.bobaoge.base.mapper.message.MessageApprenticeMapper;
import com.dc.bobaoge.common.em.MessageTemplateEnum;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreMessage;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreMessageMapper;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreMessageTypeMapper;
import com.dc.bobaoge.project.v3.to.v2.business.core.util.V3ToV2MessageCommonUtil;
import com.own.business.sync.trigger.base.entity.SyncTrigger;
import com.own.business.sync.trigger.base.method.ResolveMethod;
import com.own.component.common.exception.BusinessSimpleException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * MessageTemplate4ResolveMethod
 *
 * @author chenxueli
 * @date 2024-01-13 09:42:26
 */
@Slf4j
@Component
public class MessageTemplate4ResolveMethod implements ResolveMethod {

    @Resource
    private MessageApprenticeMapper messageApprenticeMapper;

    @Resource
    private OldCoreMessageMapper oldCoreMessageMapper;

    @Resource
    private V3ToV2MessageCommonUtil v3ToV2MessageCommonUtil;

    @Resource
    private OldCoreMessageTypeMapper oldCoreMessageTypeMapper;

    /**
     * 修改的处理方法
     * 修改的时候红包的值不能覆盖
     *
     * @param trigger 日志信息
     */
    @Override
    public void update(SyncTrigger trigger) {
        var message = messageApprenticeMapper.selectOneWithoutLogicDelete(trigger.getTargetId());
        if (message == null) {
            throw new BusinessSimpleException("消息不存在");
        }
        // 查询当前当前的标题信息，如果不存在旧版中，则不同步
        var title = oldCoreMessageTypeMapper.selectById(message.getFkTitleId());
        if (title == null) {
            return;
        }
        // 查询旧数据库中是否有这条数据
        var oldCoreMessage = oldCoreMessageMapper.selectById(trigger.getTargetId());
        if (oldCoreMessage == null) {
            // 执行新增操作
            add(trigger, message);
            return;
        }
        // 设置公共字段
        v3ToV2MessageCommonUtil.setPublicField(message, oldCoreMessage);
        // 设置消息状态信息
        v3ToV2MessageCommonUtil.setStatus(message, oldCoreMessage);
        // 查询消息的媒资信息
        v3ToV2MessageCommonUtil.setMessageAssets(message, oldCoreMessage, MessageTemplateEnum.ADVERTISEMENT);
        // 保存数据
        oldCoreMessageMapper.updateById(oldCoreMessage);
    }

    /**
     * 新增信息
     *
     * @param trigger 日志信息
     * @param message 消息信息
     */
    private void add(SyncTrigger trigger, MessageApprentice message) {
        // 构造消息信息
        var oldCoreMessage = new OldCoreMessage();
        // 设置公共字段
        v3ToV2MessageCommonUtil.setPublicField(message, oldCoreMessage);
        oldCoreMessage.setMessageHongbaoState((short) 1);
        // 设置消息状态信息
        v3ToV2MessageCommonUtil.setStatus(message, oldCoreMessage);
        // 查询消息的媒资信息
        v3ToV2MessageCommonUtil.setMessageAssets(message, oldCoreMessage, MessageTemplateEnum.ADVERTISEMENT);
        // 保存消息
        oldCoreMessageMapper.insert(oldCoreMessage);
    }

    /**
     * 名称信息
     *
     * @return 名称
     */
    @Override
    public String name() {
        return "message_template4";
    }

}
